﻿using DKT.WeiXinAPI.Factory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DKT.WeiXinAPI.Factory
{
    internal class ApiFactory
    {


        public static Dictionary<MsgType, IList<Type>> MessageRegister = new Dictionary<MsgType, IList<Type>>();
        public static Dictionary<EventType, IList<Type>> EventRegister = new Dictionary<EventType, IList<Type>>();


        public static AbstractMessage[] CreareMessage()
        {
            var context = new WeiXinApiContext();
            IList< AbstractMessage> message = new List<AbstractMessage>();

            switch (context.MessageType)
            {
                case "text":

                    if (MessageRegister.ContainsKey(MsgType.Text))
                    {
                        foreach (Type i in MessageRegister[MsgType.Text])
                        {
                            message.Add((AbstractMessage)i.Assembly.CreateInstance(i.FullName));
                        }
                    }

                    break;
                case "image":

                    if (MessageRegister.ContainsKey(MsgType.Image))
                    {
                        foreach (Type i in MessageRegister[MsgType.Image])
                        {
                            message.Add((AbstractMessage)i.Assembly.CreateInstance(i.FullName));
                        }
                    }


                    break;

                case "voice":


                    if (MessageRegister.ContainsKey(MsgType.Voice))
                    {
                        foreach (Type i in MessageRegister[MsgType.Voice])
                        {
                            message.Add((AbstractMessage)i.Assembly.CreateInstance(i.FullName));
                        }
                    }


                    break;
                case "video":


                    if (MessageRegister.ContainsKey(MsgType.Video))
                    {
                        foreach (Type i in MessageRegister[MsgType.Video])
                        {
                            message.Add((AbstractMessage)i.Assembly.CreateInstance(i.FullName));
                        }
                    }

                    break;
                case "shortvideo":



                    if (MessageRegister.ContainsKey(MsgType.Shortvideo))
                    {
                        foreach (Type i in MessageRegister[MsgType.Shortvideo])
                        {
                            message.Add((AbstractMessage)i.Assembly.CreateInstance(i.FullName));
                        }
                    }

                    break;
                case "location":


                    if (MessageRegister.ContainsKey(MsgType.Location))
                    {
                        foreach (Type i in MessageRegister[MsgType.Location])
                        {
                            message.Add((AbstractMessage)i.Assembly.CreateInstance(i.FullName));
                        }
                    }


                    break;
                case "link":


                    if (MessageRegister.ContainsKey(MsgType.Link))
                    {
                        foreach (Type i in MessageRegister[MsgType.Link])
                        {
                            message.Add((AbstractMessage)i.Assembly.CreateInstance(i.FullName));
                        }
                    }


                    break;

                case "event":
                    switch (context.WeiXinParameters["Event"])
                    {

                        case "subscribe":  //订阅

                            if (EventRegister.ContainsKey(EventType.Subscribe))
                            {
                                foreach (Type i in EventRegister[EventType.Subscribe])
                                {
                                    message.Add((AbstractMessage)i.Assembly.CreateInstance(i.FullName));
                                }
                            }

                            break;
                        case "unsubscribe": //取消订阅

                            if (EventRegister.ContainsKey(EventType.Unsubscribe))
                            {
                                foreach (Type i in EventRegister[EventType.Unsubscribe])
                                {
                                    message.Add((AbstractMessage)i.Assembly.CreateInstance(i.FullName));
                                }
                            }

                            break;
                        case "SCAN": 

                            if (EventRegister.ContainsKey(EventType.Scan))
                            {
                                foreach (Type i in EventRegister[EventType.Scan])
                                {
                                    message.Add((AbstractMessage)i.Assembly.CreateInstance(i.FullName));
                                }
                            }
                            break;

                        case "LOCATION": //报告地理位置

                            if (EventRegister.ContainsKey(EventType.Location))
                            {
                                foreach (Type i in EventRegister[EventType.Location])
                                {
                                    message.Add((AbstractMessage)i.Assembly.CreateInstance(i.FullName));
                                }
                            }


                            break;

                         
                        case "CLICK": //点击菜单，拉取消息


                            if (EventRegister.ContainsKey(EventType.Click))
                            {
                                foreach (Type i in EventRegister[EventType.Click])
                                {
                                    message.Add((AbstractMessage)i.Assembly.CreateInstance(i.FullName));
                                }
                            }

                            break;

                        case "VIEW":  //点击菜单跳转链接时的事件推送


                            if (EventRegister.ContainsKey(EventType.View))
                            {
                                foreach (Type i in EventRegister[EventType.View])
                                {
                                    message.Add((AbstractMessage)i.Assembly.CreateInstance(i.FullName));
                                }
                            }


                            break;


                    }
                    break;
            }


            foreach (var m in message)
            {
                m.Context = context;
            }

          
            return message.ToArray();

        }
    }
}